/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tickets.tickets;

import tickets.ticketRegister.TicketRegister;
import java.util.Date;

/**
 * Facade class for making operations with tickets
 * @author Hlib_Babii
 */
public class TicketPurchaseSystem {

    private int travelPrice;
    
    private TicketRegister ticketRegister;
    
    private static long currentId = 0;
    
    protected static long getNextId() {
        return currentId++;
    }

    public TicketPurchaseSystem(TicketRegister ticketRegister, int travelPrice) {
        this.ticketRegister = ticketRegister;
        this.travelPrice = travelPrice;
    }

    public void setTravelPrice(int travelPrice) {
        if (travelPrice >= 0) {
            this.travelPrice = travelPrice;
        } else {
            throw new IllegalArgumentException();
        }
    }

    /**
     * Put specified amount of money to the specified ticket
     * @param ticketId
     * @param ammount
     * @throws WrongTicketTypeException when ticket is not LimitedMoneyTicket
     */
    public void putMoney(long ticketId, int ammount) throws WrongTicketTypeException {
        if (ammount >= 0) {
            Ticket ticket = ticketRegister.getTicketById(ticketId);
            if (ticket != null) {
                ticket.putMoney(ammount);
            } else {
                throw new IllegalArgumentException();
            }
        } else {
            throw new IllegalArgumentException();
        }
    }

    // creating diff types of tickets
    
    public Ticket createStudentLimitedTravelsTicket(int nTravels) {
        if (nTravels >= 0) {
            Ticket ticket = new LimitedTravelsTicket(new StudentsTicket(getNextId()), nTravels);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createStudentLimitedDurationTicket(Date expirationDate) {
        if (expirationDate.getTime() > System.currentTimeMillis()) {
            Ticket ticket = new LimitedDurationTicket(new StudentsTicket(getNextId()), expirationDate);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createSchoolkidsLimitedTravelsTicket(int nTravels) {
        if (nTravels >= 0) {
            Ticket ticket = new LimitedTravelsTicket(new SchoolkidsTicket(getNextId()), nTravels);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createSchoolkidsLimitedDurationTicket(Date expirationDate) {
        if (expirationDate.getTime() > System.currentTimeMillis()) {
            Ticket ticket = new LimitedDurationTicket(new SchoolkidsTicket(getNextId()), expirationDate);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createUsualLimitedTravelsTicket(int nTravels) {
        if (nTravels >= 0) {
            Ticket ticket = new LimitedTravelsTicket(new UsualTicket(getNextId()), nTravels);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createUsualLimitedDurationTicket(Date expirationDate) {
        if (expirationDate.getTime() > System.currentTimeMillis()) {
            Ticket ticket = new LimitedDurationTicket(new UsualTicket(getNextId()), expirationDate);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }

    public Ticket createUsualLimitedMoneyTicket(int ammount) {
        if (ammount >= 0) {
            Ticket ticket = new LimitedMoneyTicket(new UsualTicket(getNextId()), travelPrice, ammount);
            ticketRegister.addTicket(ticket);
            return ticket;
        }
        throw new IllegalArgumentException();
    }
}
